﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TYBase;

namespace EVEPreChargeDataMonitor
{
    public class TxtDataHelper
    {
        KeyValuePair<string, string> GetValue(string line)
        {
            Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
            keyValuePairs.FirstOrDefault();
            var keyValue = line.Split('=');
            if (keyValue.Length == 2)
            {
                return new KeyValuePair<string, string>(keyValue[0], keyValue[1]);
            }
            return new KeyValuePair<string, string>("", ""); ;
        }

        string flag_type = "type";
        string flag_lot = "lot";
        string flag_cell = "cell";
        string currentType = "";

        public void FileToData(string filePath, out Tray tray)
        {
            string trayName = Path.GetFileNameWithoutExtension(filePath);
            tray = new Tray(trayName);
            var lines = File.ReadAllLines(filePath);


            foreach (var line in lines)
            {
                if (line.StartsWith("[TYPE]"))
                {
                    currentType = flag_type;
                    continue;
                }
                else if (line.StartsWith("[LOT]"))
                {
                    currentType = flag_lot;
                    continue;
                }
                else if (line.StartsWith("[CELL]"))
                {
                    currentType = flag_cell;
                    continue;
                }
                var keyValue = GetValue(line);
                if (!string.IsNullOrEmpty(keyValue.Key))
                {
                    continue;
                }
                if (currentType == flag_type)
                {
                    tray.TypeId = keyValue.Value;
                    continue;
                }
                if (currentType == flag_lot)
                {
                    tray.LotId = keyValue.Value;
                    continue;
                }
                if (currentType == flag_cell)
                {
                    var key = keyValue.Key.AsInt();
                    var value = keyValue.Value.AsInt();
                    if (key > 0)
                        tray.Cells[key] = value;
                    continue;
                }
            }
        }
    }
}
